package JZ;

/**
 * @Classname JZ68I
 * @Description // https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-zui-jin-gong-gong-zu-xian-lcof/ 树
 * @Date 2021/4/21 15:38
 * @Created by yin
 */
public class JZ68I {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null) return null;
        if (root == p || root == q) return root;
        TreeNode left = lowestCommonAncestor(root.left, p, q);
        TreeNode right = lowestCommonAncestor(root.right, p, q);
        if (left != null && right != null) { // pq在左右两边
            return root;
        }
        if (left != null) { // pq都在左边
            return left;
        }
        return right;
    }
}
